/*
 This file is part of js-cellularautomata.

    js-cellularautomata is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    js-cellularautomata is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with js-cellularautomata.  If not, see <http://www.gnu.org/licenses/>.

*/
var globindex=1;

function rule1(i1,i3) {
    var i2=1;
    if (i1==0 && i3==1)
        i2=0;
    return i2;
}
function rule2(i1,i3) {
    var i2=1;
    if (i1==1 && i3==0)
        i2=0;
    return i2;
}

function rule3(i1,i3) {
    var i2=1;
    if (i1==i3)
        i2=0;
    return i2;
}




function printOneRow(theoldrow,ruleFunc){
    var newRowVals = Array();

    var htmlstr = '';
    var newrow=Array();
    htmlstr += '<tr>';
    for (var ii=0;ii<theoldrow.length;ii++){
        if (ii==0) prevcell=0;
        else prevcell = theoldrow[ii-1];
        
        if (ii==theoldrow.length-1) nextcell=0;
        else nextcell=theoldrow[theoldrow.length-1];
        
        curcell = ruleFunc(prevcell, nextcell);
        htmlstr += '<td class="cell'+curcell+'"></td>';
        newrow[ii]=curcell;
    }

    htmlstr += '<tr/>';


    newRowVals[0]  = newrow;
    newRowVals[1]  = htmlstr;

    return newRowVals;
}


function printFirstRow(firstrow){
    var htmlstr = '';
    htmlstr += '<tr>';
    for (var ii=0;ii<firstrow.length;ii++){
        htmlstr += '<td class="cell'+firstrow[ii]+'"></td>';
    }
    htmlstr += '<tr/>';
    return htmlstr;
}
function printRows(Nrows,Ncols,ruleFunc){
  thestr = '';
  currow = Array();
  // determine mid point to start off a '1' cell with
  var midPoint = Ncols/2;
  if (Ncols % 2 == 1) midPoint = (Ncols-1)/2;
  
  for (var jj=0;jj<Ncols;jj++) {
    if (jj==midPoint) currow[jj]=1;
    else currow[jj]=0;
  }
  
  
  // currow = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
  thestr +='<table class="automaton">';
  thestr += printFirstRow(currow);
  for (var ii=0;ii<Nrows;ii++){
      currowVals = printOneRow(currow,ruleFunc);
      currow  = currowVals[0];
      thestr += currowVals[1];
  } 
  thestr +='</table>';
  
  
  return thestr;
}

//printRows(1000,rule1);

//printRows(1000,rule2);

function reDrawAutomaton(theSel) {
    if(!theSel){
        theSel =  document.getElementById('automatonSel');
    }
    // get the function handle with the same name as the option
    theFunc = eval(theSel.options[theSel.selectedIndex].value);
    Nrows = document.getElementById('rowsize').value;
    Ncols = document.getElementById('colsize').value;
    
    document.getElementById("automatonDiv").innerHTML = printRows(Nrows,Ncols,theFunc);
}
